local Msg = require("core.Msg")
local QiliaoConfig = require("excel.Qiliao")
local YaoshuConfig = QiliaoConfig.yaoshu
local BufferConfig = require("excel.Buffer") 
local Buffer = BufferConfig.Buff
local BufferCmd = BufferConfig.BuffCmd
local Grid = require("bag.Grid")
local ObjHuman = require("core.ObjHuman")
local BagLogic = require("bag.BagLogic")
local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local SkillConfig = require("excel.Skill").Skill
local SkillNotify  = require("skill.Notify")
local BufferLogic   = require("buffer.Buffer")
local OfficialLogic = require("official.OfficialLogic")

--组织要术信息
function setYaoshuInfo(human, info, id)
  local curYSConfig = QiliaoConfig[YaoshuConfig[id].mingzi] 
  info.id = id
  info.name = YaoshuConfig[id].name
  info.image = YaoshuConfig[id].bufImage
  local data = ObjHuman.getYaoshu(human, id)
  local cache = {}
  if data then
    info.status = data.status
    info.power = curYSConfig[data.skillLv].addPower    
    if curYSConfig[data.skillLv + 1] then
      info.needItem[0] = #curYSConfig[data.skillLv + 1].needItem 
      for i=1, info.needItem[0] do 
        Grid.create(cache, curYSConfig[data.skillLv + 1].needItem[i][1], curYSConfig[data.skillLv + 1].needItem[i][2])
        Grid.makeItem(cache, info.needItem[i], 0, human)
       end
       info.nextPower = curYSConfig[data.skillLv + 1].addPower
       info.needLv =  curYSConfig[data.skillLv + 1].needLv
    else
      info.needItem[0] = 0
      info.nextPower = 0
      info.needLv = 0
    end
    info.needYinliang = curYSConfig[data.skillLv].needYinliang
    info.lv = data.skillLv
  else
    info.status = 0
    info.power = curYSConfig[1].addPower
    info.nextPower = curYSConfig[2].addPower
    info.needLv =  curYSConfig[1].needLv
    info.needItem[0] = #curYSConfig[1].needItem    
    for i=1, info.needItem[0] do 
      Grid.create(cache, curYSConfig[1].needItem[i][1], curYSConfig[1].needItem[i][2])
      Grid.makeItem(cache, info.needItem[i], 0, human)
     end
     info.needYinliang = curYSConfig[1].needYinliang
     info.lv = 0
  end
  local bufferLv = data and data.skillLv or 1
  local addBuffer = curYSConfig[bufferLv].addBuffer
  info.desc[0] = 2
  info.desc[1] = curYSConfig[bufferLv].desc
  info.desc[2] = ""
--  for i=1,#BufferCmd[Buffer[addBuffer].cmd].desc do
--    local arg = Buffer[addBuffer].arg[i]
--    if string.find(BufferCmd[Buffer[addBuffer].cmd].desc[i], "%%") then
--      arg = arg * 100
--    end
--    if info.desc[1] ~= "" then
--      info.desc[1] = info.desc[1] ..",".. string.gsub(BufferCmd[Buffer[addBuffer].cmd].desc[i], "#d", arg)
--    else
--      info.desc[1] = "" .. string.gsub(BufferCmd[Buffer[addBuffer].cmd].desc[i], "#d", arg)
--    end
--  end
  if curYSConfig[bufferLv + 1] then
    info.desc[2] = curYSConfig[bufferLv + 1].desc
--    for i=1,#BufferCmd[Buffer[curYSConfig[bufferLv].addBuffer].cmd].desc do
--      local arg = Buffer[curYSConfig[bufferLv + 1].addBuffer].arg[i]
--      if string.find(BufferCmd[Buffer[curYSConfig[bufferLv + 1].addBuffer].cmd].desc[i], "%%") then
--        arg = arg * 100
--      end
--      if info.desc[2] ~= "" then 
--        info.desc[2] = info.desc[2] ..",".. string.gsub(BufferCmd[Buffer[curYSConfig[bufferLv + 1].addBuffer].cmd].desc[i], "#d", arg)
--      else
--        info.desc[2] = "".. string.gsub(BufferCmd[Buffer[curYSConfig[bufferLv + 1].addBuffer].cmd].desc[i], "#d", arg)
--      end    
--    end
  else
    info.desc[0] = 1
  end
end
function getQiliaoPower(human)
  if human.db.combatYaoshu == 0 then
    return human.db.QiliaoSkillLv * 50
  end
  local data = ObjHuman.getYaoshu(human, human.db.combatYaoshu)
  local curYSConfig = QiliaoConfig[YaoshuConfig[human.db.combatYaoshu].mingzi]
  return human.db.QiliaoSkillLv * 50 + (data and curYSConfig[data.skillLv].addPower or 0)
end
--设置气疗信息
function setQiliaoSkill(human, info)
  local cache = {}
  SkillNotify.setSkillData(human, QiliaoConfig.other[1].skillId, info.skill, human.db.QiliaoSkillLv)
  local len = 0
  local condition = {}
  if human.db.QiliaoSkillLv == 0 then
    condition = SkillConfig[QiliaoConfig.other[1].skillId].activationCondition
  else
    condition = SkillConfig[QiliaoConfig.other[1].skillId].upgradeCondition[human.db.QiliaoSkillLv]
  end
  for k,v in ipairs (condition) do
    if v[1] == "item" then
      len = len + 1
      Grid.create(cache, v[2], v[3])
      Grid.makeItem(cache, info.skillItem[len], 0, human)
    end
  end
  info.skillItem[0] = len
  info.skillPower = getQiliaoPower(human)
end
--更新要术
function updateYaoshu(human, id)
  local sendMsg = Msg.gc.GC_UPDATE_YAOSHU
  setYaoshuInfo(human, sendMsg.info, id)
  setQiliaoSkill(human, sendMsg)
  Msg.send(sendMsg, human.fd)
end
--发送要术列表
function sendYaoshuList(human)
  local sendMsg = Msg.gc.GC_YAOSHU_LIST
  sendMsg.info[0] = #YaoshuConfig
  for i=1, #YaoshuConfig do
    setYaoshuInfo(human, sendMsg.info[i], i)
  end
  setQiliaoSkill(human, sendMsg)
  Msg.send(sendMsg, human.fd)
end

function setStatus(human, data)
    if data.status == 2 then
        data.status = 1
        if data.id == human.db.combatYaoshu then
            human.db.combatYaoshu = 0
        end
    elseif data.status == 1 then
        data.status = 2
        if not data.id then
            assert(nil)
        end
        human.db.combatYaoshu = data.id
    end
end
--切换气疗状态
function changeStatus(human, id)
    local data = ObjHuman.getYaoshu(human, id)
    if not data then
        return
    end
    local combatData
    local combatId = human.db.combatYaoshu
    if human.db.combatYaoshu ~= 0 and human.db.combatYaoshu ~= id then
        combatData = ObjHuman.getYaoshu(human, human.db.combatYaoshu)
        setStatus(human, combatData)
        updateYaoshu(human, combatId)
    end
    setStatus(human, data)
    updateYaoshu(human, id)
    ObjHuman.doCalc(human)
    SkillNotify.GC_SKILL_LIST(human)
    SkillNotify.GC_SKILL_LIST_CHANGE(human)
    SkillNotify.GC_SKILL_MAIN_MENU(human)
end
--升级气疗主技能
function upgradeQiliao(human)
  if human.db.QiliaoSkillLv == 0 then
    return
  end
  if not SkillConfig[QiliaoConfig.other[1].skillId].upgradeCondition[human.db.QiliaoSkillLv] then
    return
  end
  for k,v in ipairs (SkillConfig[QiliaoConfig.other[1].skillId].upgradeCondition[human.db.QiliaoSkillLv]) do
    if v[1] == "item" then
      if BagLogic.getItemCount(human, v[2]) < v[3] then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.ITEM_NOT_ENOUGH)
      end
    elseif v[1] == "money" then
      if ObjHuman.getYinliang(human) < v[2] then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YINLIANG_NOT_ENOUGH)
      end
    elseif v[1] == "lv" then
      if human.db.lv < v[2] then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.HUMAN_LV_NOT_ENOUGH)
      end 
    end
  end
  for k,v in pairs (SkillConfig[QiliaoConfig.other[1].skillId].upgradeCondition[human.db.QiliaoSkillLv]) do
    if v[1] == "item" then
      BagLogic.delItemsById(human, v[2], v[3], "qiliao")
    elseif v[1] == "money" then
      ObjHuman.decYinliang(human, v[2], "qiliao") 
    end    
  end
  human.db.QiliaoSkillLv = human.db.QiliaoSkillLv + 1
  OfficialLogic.checkOneCondition(human, "qiliaoLv")
  updateYaoshu(human, 1)
  ObjHuman.doCalc(human)
  SkillNotify.GC_SKILL_LIST(human)
  SkillNotify.GC_SKILL_LIST_CHANGE(human)
  SkillNotify.GC_SKILL_MAIN_MENU(human)
end

--激活气疗主技能
function jihuoQiliao(human)
  if human.db.QiliaoSkillLv ~= 0 then
    return
  end
  for k,v in ipairs (SkillConfig[QiliaoConfig.other[1].skillId].activationCondition) do
    if v[1] == "item" then
      if BagLogic.getItemCount(human, v[2]) < v[3] then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.ITEM_NOT_ENOUGH)
      elseif v[1] == "money" then
        if ObjHuman.getYinliang(human) < v[2] then
          return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YINLIANG_NOT_ENOUGH)
        end
      elseif v[1] == "lv" then
        if human.db.lv < v[2] then
          return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.HUMAN_LV_NOT_ENOUGH)
        end 
      end
    end
  end
  for k,v in pairs (SkillConfig[QiliaoConfig.other[1].skillId].activationCondition) do
    if v[1] == "item" then
      BagLogic.delItemsById(human, v[2], v[3], "qiliao")
    elseif v[1] == "money" then
      ObjHuman.decYinliang(human, v[2], "qiliao") 
    end    
  end
  human.db.QiliaoSkillLv = 1
  updateYaoshu(human, 1)
  ObjHuman.doCalc(human)
  SkillNotify.GC_SKILL_LIST(human)
  SkillNotify.GC_SKILL_LIST_CHANGE(human)
  SkillNotify.GC_SKILL_MAIN_MENU(human)
end
--升级要术技能
function upgradeYaoshu(human, id)
  local data = ObjHuman.getYaoshu(human, id)
  if not data then
    return
  end
  local curYSConfig = QiliaoConfig[YaoshuConfig[id].mingzi]
  if not curYSConfig[data.skillLv + 1] then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YAOSHU_SKILL_MAX_LV)
  end  
  if human.db.QiliaoSkillLv < curYSConfig[data.skillLv + 1].needLv then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.QILIAO_SKILL_LV_NOT_ENOUGH)
  end
  for i=1, #curYSConfig[data.skillLv + 1].needItem do 
    if BagLogic.getItemCount(human, curYSConfig[data.skillLv + 1].needItem[i][1]) < curYSConfig[data.skillLv + 1].needItem[i][2] then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.ITEM_NOT_ENOUGH)
    end
  end
  if ObjHuman.getYinliang(human) < curYSConfig[data.skillLv + 1].needYinliang then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YINLIANG_NOT_ENOUGH)
  end
  for i=1, #curYSConfig[data.skillLv + 1].needItem do 
    BagLogic.delItemsById(human, curYSConfig[data.skillLv + 1].needItem[i][1], curYSConfig[data.skillLv + 1].needItem[i][2], "qiliao")
  end
  ObjHuman.decYinliang(human, curYSConfig[data.skillLv + 1].needYinliang, "qiliao")
  data.skillLv = data.skillLv + 1
  updateYaoshu(human, id)
  if human.db.combatYaoshu == id then
    ObjHuman.doCalc(human)
  end
end
--激活要术技能
function jihuoYaoshu(human, id)
  local data = ObjHuman.getYaoshu(human, id)
  if data then
    return
  end
  local curYSConfig = QiliaoConfig[YaoshuConfig[id].mingzi]
  if human.db.QiliaoSkillLv < curYSConfig[1].needLv then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.QILIAO_SKILL_LV_NOT_ENOUGH)
  end
  for i=1, #curYSConfig[1].needItem do 
    if BagLogic.getItemCount(human, curYSConfig[1].needItem[i][1]) < curYSConfig[1].needItem[i][2] then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.ITEM_NOT_ENOUGH)
    end
  end
  if ObjHuman.getYinliang(human) < curYSConfig[1].needYinliang then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YINLIANG_NOT_ENOUGH)
  end
  for i=1, #curYSConfig[1].needItem do 
    BagLogic.delItemsById(human, curYSConfig[1].needItem[i][1], curYSConfig[1].needItem[i][2], "qiliao")
  end
  ObjHuman.decYinliang(human, curYSConfig[1].needYinliang, "qiliao")
  ObjHuman.addYaoshu(human, id)
  if human.db.yaoshuListLen == 1 then
    changeStatus(human, id)
  end
  updateYaoshu(human, id)
end

function getQiliaoSkill(human)
  if human.db.QiliaoSkillLv ~= 0 then
      return QiliaoConfig.other[1].skillId, human.db.QiliaoSkillLv
  end  
end
--使用气疗技能
function onUse(human, skillID)
  if QiliaoConfig.other[1].skillId ~= skillID then
    return
  end
  if human.db.QiliaoSkillLv <= 0 or human.db.combatYaoshu == 0 then
    return
  end
  local curYSConfig = QiliaoConfig[YaoshuConfig[human.db.combatYaoshu].mingzi]
  local data = ObjHuman.getYaoshu(human, human.db.combatYaoshu)
  BufferLogic.add(curYSConfig[data.skillLv].addBuffer, human, human)
end